﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Content.ModelLink
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
            // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ContentAdmin", context.Request["ColumnID"]);
            //if (auth != "ACCEPT")
            //  {
            //      return;
            //  }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加友情链接模型的内容
            if (dowhat == "add_link")
            {
                string ColumnID = context.Request["ColumnID"];

                //发布者和最后编辑者
                string FirstEditor = UserName;
                string LastEditor = UserName;

                string WebUrl = context.Request["WebUrl"];
                string WebName = context.Request["WebName"];
                string WebLogo = context.Request["WebLogo"];
                string UpdateDate = DateTime.Now.ToString();
               

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Create.InsertLink(ColumnID, FirstEditor, LastEditor, WebUrl, WebName, WebLogo, UpdateDate) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加友情链接【" + Name + "】" + WebName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 修改友情链接模型的内容
            if (dowhat == "update_link")
            {
                string ID = context.Request["ID"];
                string ColumnID = context.Request["ColumnID"];

                // 修改最后编辑者
                string LastEditor = UserName;

                string WebUrl = context.Request["WebUrl"];
                string WebName = context.Request["WebName"];
                string WebLogo = context.Request["WebLogo"];
                string UpdateDate = DateTime.Now.ToString();
               

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Update.UpdateLink(ID, ColumnID, LastEditor, WebUrl, UpdateDate,WebName, WebLogo) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改友情链接栏目【" + Name + "】" + WebName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除友情链接模型的内容
            if (dowhat == "delete_link")
            {
                string ID = context.Request["ID"];

                string WebName = BLL.Find.FindLink(ID).WebName;
                string Name = BLL.Find.FindOneColumn(context.Request["ColumnID"]).Name;

                if (BLL.Delete.DeleteLink(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除友情链接模型栏目【" + Name + "】" + WebName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}